import * as elementui from 'element-ui'

elementui.MessageBox._confirm = elementui.MessageBox.confirm
elementui.MessageBox.confirm = (content, title, props) => {
  props = Object.assign({
    customClass: 'dialog'
  }, props)
  return elementui.MessageBox._confirm(content, title, props)
}

elementui.Upload.props.action.required = false
elementui.Upload.props.action.default = '/api/upload/image'

elementui.Carousel.beforeMount = function () { // 修正轮播图片在表单BUTTON提交的行为
  this.$nextTick(function () {
    Array.prototype.slice.call(this.$el.querySelectorAll('button')).forEach(function (item) {
      item.type = 'button'
    })
  })
}

elementui.Carousel.props.trigger.default = 'click'

elementui.Carousel.props.slides = {
  type: Number,
  default: 1
}

elementui.Form.props.labelWidth.default = '142px'

elementui.Pagination.props.layout.default = 'prev, pager, next, sizes, jumper, total'
elementui.Pagination.props.pageSizes.default = () => {
  return [15, 20, 30, 40, 50]
}
/*
*重写InputNumber默认行为
*/
elementui.InputNumber.props.controls = false // 默认不是控制按钮
elementui.InputNumber.props.debounce.default = 380
elementui.InputNumber.props.max.default = 100000
elementui.InputNumber.props.bit = {type: Number}
elementui.InputNumber.created = () => {}
elementui.InputNumber.beforeMount = function () {
  this.$nextTick(() => {
    var self = this
    this.$el.addEventListener('keydown', function (e) {
      e.keyCode === 13 && self.handleBlur(e)
    }, false)
    this.$el.addEventListener('focusin', function (e) {
      e.target.select() // 得到焦点选中值
    }, false)
  })
}
elementui.InputNumber.methods.debounceHandleInput = function () {}
elementui.InputNumber.methods.handleBlur = function (e) {
  var value = Number(e.target.value)
  if (!isNaN(value)) {
    let bValue = this.bit || 1
    let newValue = Math.round(1 / bValue * value) / (1 / bValue)
    this.setCurrentValue(newValue)
  } else {
    this.$refs.input.setCurrentValue(this.currentValue)
  }
}

elementui.DatePicker.props.editable = false
elementui.DatePicker.props.format = {
  type: String,
  default: 'yyyy.MM.dd'
}

elementui.DatePicker.updated = function () { // 对format ww 月周扩展
  let value = this.currentValue
  if (value && this.type === 'week' && this.format.indexOf('ww') > -1) {
    this.$nextTick(function () {
      var getMonthWeek = function (a, b, c) {
        var date = new Date(a, parseInt(b), c)
        var w = date.getDay()
        var d = date.getDate()
        return Math.ceil((d + 6 - w) / 7)
      }
      let currentValue = this.$el.querySelector('input').value
      this.$el.querySelector('input').value = currentValue.replace('ww', getMonthWeek(value.getFullYear(), value.getMonth(), value.getDate()))
    })
  }
}

console.log('elementui', elementui)
export default elementui
